package helloworld.admin.form.news;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.ImageButtonBean;
import org.apache.struts.util.MessageResources;
import helloworld.example.util.BaseActionForm;

/**
 * @author TUYEN-DM
 *         <p>
 *         2009/05/25
 *         </p>
 *         <p>
 *         NewsForm is the object representation of the html form for entering
 *         news's name in helloworld.
 *         </p>
 */
public class NewsForm extends BaseActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** News title */
	private String newsTitle;
	private String header;
	private String body;
	private String postDateTime;
	private String source;

	private int newsID;

	private ImageButtonBean save;
	private ImageButtonBean cancel;
	private boolean isExisting;

	public String getNewsTitle() {
		return newsTitle;
	}

	public void setNewsTitle(String newsTitle) {
		this.newsTitle = newsTitle;
	}

	public String getHeader() {
		return header;
	}

	public void setHeader(String header) {
		this.header = header;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getPostDateTime() {
		return postDateTime;
	}

	public void setPostDateTime(String postDateTime) {
		this.postDateTime = postDateTime;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public NewsForm() {
		// Don't forget to Initialize here
		// Also don't forget to init in reset();
		init();
	}

	// Without this method to initialization, non-primitive objects such as
	// Address, ImageButtonBean will throw NullPointerException
	// or might not work as expected
	// since a getAddress(), getSave() etc. returns a null object
	protected void init() {
		newsTitle = "";
		header = "";
		body = "";
		postDateTime = "";
		source = "";
		save = new ImageButtonBean();
		cancel = new ImageButtonBean();
		newsID = 0;
		isExisting = false;
	}

	public int getNewsID() {
		return newsID;
	}

	public void setNewsID(int i) {
		newsID = i;
	}

	public ImageButtonBean getCancel() {
		return cancel;
	}

	public ImageButtonBean getSave() {
		return save;
	}

	public boolean getIsExisting() {
		return isExisting;
	}

	public void setIsExisting(boolean b) {
		isExisting = b;
	}

	/**
	 * This method is called by the RequestProcessor immediately after
	 * populating the NewsForm only if the validate=true flag is set in the
	 * helloworld-config.xml Do validation here and return ActionErrors if any,
	 * to the RequestProcessor
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		// Need to do this explicitly now. Since the Button is not grey and its
		// name is not
		// org.apache.struts...Cancel anymore, and the validate=true still
		// remains in ActionMapping, the call to bypass validation on clicking
		// cancel image
		// has to be performed manually
		if (getCancel().isSelected()) {
			return errors;
		}

		MessageResources msgRes = (MessageResources) request
				.getAttribute(Globals.MESSAGES_KEY);

		// NewsTitle cannot be empty
		if (newsTitle == null || newsTitle.trim().equals("")) {
			String newsTitle = msgRes.getMessage("news.newstitle");
			String[] rplcmntValueArr = { newsTitle };
			ActionError err = new ActionError("error.required", rplcmntValueArr);
			errors.add("newsTitle", err);
		}
		return errors;
	}

}